﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WeiTalk.SinaEntities.Status;
using System.Windows.Media.Imaging;

namespace WeiTalk.Controls
{
    public delegate void ClickEventHandler(object sender,EventArgs e);
    public partial class WeiBoControl : UserControl
    {
        public static readonly DependencyProperty UserNameProperty =
        DependencyProperty.Register(
        "UserName", typeof(string),
        typeof(WeiBoControl), null
        );
        public string UserName
        {
            get
            {
                return (string)GetValue(UserNameProperty);
            }
            set
            {
                SetValue(UserNameProperty, value);
            }
        }

        public static readonly DependencyProperty CreatedAtProperty =
        DependencyProperty.Register(
        "CreatedAt", typeof(string),
        typeof(WeiBoControl), null
        );
        public string CreatedAt
        {
            get
            {
                return (string)GetValue(CreatedAtProperty);
            }
            set
            {
                SetValue(CreatedAtProperty, value);
            }
        }

        public static readonly DependencyProperty ProfileImageUrlProperty =
        DependencyProperty.Register(
        "ProfileImageUrl", typeof(ImageSource),
        typeof(WeiBoControl), null
        );
        public ImageSource ProfileImageUrl
        {
            get
            {
                return (ImageSource)GetValue(ProfileImageUrlProperty);
            }
            set
            {
                SetValue(ProfileImageUrlProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(
        "Text", typeof(string),
        typeof(WeiBoControl), null
        );
        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set {
                SetValue(TextProperty,value);
            }
        }

        public static readonly DependencyProperty RetweetedContentProperty =
            DependencyProperty.Register("RetweetedContent", typeof(string), typeof(WeiBoControl), null);
        public string RetweetedContent
        {
            get { return (string)GetValue(RetweetedContentProperty); }
            set { SetValue(RetweetedContentProperty, value); }
        }

        public static readonly DependencyProperty RetweetedUserNameProperty =
            DependencyProperty.Register("RetweetedUserName", typeof(string), typeof(WeiBoControl), null);
        public string RetweetedUserName
        {
            get { return (string)GetValue(RetweetedUserNameProperty); }
            set { SetValue(RetweetedUserNameProperty, value); }
        }

        public static readonly DependencyProperty RetweetedCreateAtProperty =
           DependencyProperty.Register("RetweetedCreateAt", typeof(string), typeof(WeiBoControl), null);
        public string RetweetedCreateAt
        {
            get { return (string)GetValue(RetweetedCreateAtProperty); }
            set { SetValue(RetweetedCreateAtProperty, value); }
        }

        public static readonly DependencyProperty ThumbnailPicProperty =
          DependencyProperty.Register("ThumbnailPic", typeof(ImageSource), typeof(WeiBoControl), null);
        public ImageSource ThumbnailPic
        {
            get { return (ImageSource)GetValue(ThumbnailPicProperty); }
            set { SetValue(ThumbnailPicProperty, value); }
        }

        public static readonly DependencyProperty RetweetedThumbnailPicProperty =
          DependencyProperty.Register("RetweetedThumbnailPic", typeof(ImageSource), typeof(WeiBoControl), null);
        public ImageSource RetweetedThumbnailPic
        {
            get { return (ImageSource)GetValue(RetweetedThumbnailPicProperty); }
            set { SetValue(RetweetedThumbnailPicProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
          DependencyProperty.Register("Source", typeof(string), typeof(WeiBoControl), null);
        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty RetweetedSourceProperty =
          DependencyProperty.Register("RetweetedSource", typeof(string), typeof(WeiBoControl), null);
        public string RetweetedSource
        {
            get { return (string)GetValue(RetweetedSourceProperty); }
            set { SetValue(RetweetedSourceProperty, value); }
        }

        public string RetweetedCommentCount
        { get; set; }

        public string RetweetedRepostsCount
        { get; set; }

        public string CommentCount
        { get; set; }

        public string RepostsCount
        {
            get;
            set;
        }

        public string Id
        { get; set; }

        public string UserId
        { get; set; }

        public string RetweetedId
        {
            get;
            set;
        }

        public string RetweetedUserId
        {
            get;
            set;
        }

        public Storyboard ClickStoryboard
        {
            get { return sbSelected; }
        }

        //public ClickEventHandler Click;
        public event ClickEventHandler Click;
        double perMouseY = 0d;
        double perMouseX = 0d;

        public WeiBoControl()
        {
            InitializeComponent();
            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            spRetweeted.Visibility = System.Windows.Visibility.Collapsed;
            tbUserName.Text = UserName;
            tbCreateAt.Text = CreatedAt;

            tbSource.Text = Source;
            tbRetweetedSource.Text = RetweetedSource;
            tbRepostsCount.Text = "转发: "+ RepostsCount;
            tbCommentsCount.Text ="评论: "+ CommentCount;
            if (ProfileImageUrl != null)
            {
                try
                {
                    headImg.Source = ProfileImageUrl;
                    ((BitmapImage)headImg.Source).ImageFailed += new EventHandler<ExceptionRoutedEventArgs>((s, er) => {
                        ((BitmapImage)s).UriSource = new Uri("/Images/default.png", UriKind.Relative); 
                    });
                }
                catch { }
            }
            if (ThumbnailPic != null)
            {
                try
                {
                    imgWeiBo.Source = ThumbnailPic;
                }
                catch { }
            }
            else
            {
                imgWeiBo.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (!String.IsNullOrEmpty(RetweetedContent))
            {
                spRetweeted.Visibility = System.Windows.Visibility.Visible;
                tbRetweetedContent.Text = RetweetedContent;

                if (!String.IsNullOrEmpty(RetweetedUserName))
                {
                    tbRetweetedUserName.Text = "@"+ RetweetedUserName;
                    tbRetweetedCreateAt.Text = RetweetedCreateAt;
                }
                if (RetweetedThumbnailPic != null)
                {
                    try
                    {
                        imgRetweetedWeiBo.Source = RetweetedThumbnailPic;
                    }
                    catch { }
                }
                else
                {
                    imgRetweetedWeiBo.Visibility = System.Windows.Visibility.Collapsed;
                }
                if (!String.IsNullOrEmpty(RetweetedCommentCount))
                {
                    tbRetweetedCommentsCount.Text = "评论: " + RetweetedCommentCount;
                }
                else
                {
                    tbRetweetedCommentsCount.Text = "";
                }
                if (!String.IsNullOrEmpty(RetweetedRepostsCount))
                {
                    tbRetweetedRepostsCount.Text = "转发: " + RetweetedRepostsCount;
                }
                else
                {
                    tbRetweetedRepostsCount.Text = "";
                }
            }
            else
            {
                spRetweeted.Visibility = System.Windows.Visibility.Collapsed;
            }
            tbContent.Text = Text;
            this.Visibility = System.Windows.Visibility.Visible;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            perMouseY = e.GetPosition(null).Y;
            perMouseX = e.GetPosition(null).X;
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Click != null)
            {
                if (Math.Abs(perMouseY - e.GetPosition(null).Y) < 10 &&
                    Math.Abs(perMouseX - e.GetPosition(null).X) < 50)
                {
                    OnClick(e);
                }
            }
        }

        private void OnClick(EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }
    }
}
